package com.hb.trade.server.expert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.expert.dto.ExpertGroupDTO;
import com.hb.core.pojo.expert.dto.ExpertGroupMembersDTO;
import com.hb.core.pojo.expert.entity.ExpertGroup;
import com.hb.core.pojo.expert.entity.ExpertGroupMembers;
import com.hb.core.pojo.expert.mapper.ExpertGroupMapper;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.server.expert.service.ExpertGroupMembersService;
import com.hb.trade.server.expert.service.ExpertGroupService;
import org.fzzn.core.util.Enums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @author ljs
 * @date 2022-2-10 14:13:46
 **/
@Service
public class ExpertGroupServiceImpl extends ServiceImpl<ExpertGroupMapper, ExpertGroup> implements ExpertGroupService {

    @Autowired
    private ExpertGroupMembersService expertGroupMembersService;

    @Override
    @Transactional
    public boolean expertGroupSave(ExpertGroupDTO expertGroupDTO) {
        expertGroupDTO.buildBaseData(1);
        boolean saveGroupResult = this.save(expertGroupDTO);
        List<ExpertGroupMembersDTO> expertList = expertGroupDTO.getExpertList();
        List<ExpertGroupMembers> membersList = new ArrayList<>();
        for (ExpertGroupMembers expertGroupMembers : expertList) {
            expertGroupDTO.to(ExpertGroup.class);
            expertGroupMembers.setId(null);
            expertGroupMembers.setExpertMajor(expertGroupMembers.getMajorCode());
            expertGroupMembers.setPhoneNum(expertGroupMembers.getPhoneNumber());
            expertGroupMembers.buildBaseData(1);
            expertGroupMembers.setGroupId(expertGroupDTO.getId());
            membersList.add(expertGroupMembers);
        }
        boolean saveMemberResult = expertGroupMembersService.saveBatch(membersList);
        return saveMemberResult && saveGroupResult;
    }

    /**
     * 查询专家组列表
     *
     * @param expertGroupDTO
     * @return
     */
    @Override
    public PageResult<ExpertGroupDTO> listExpertGroup(ExpertGroupDTO expertGroupDTO) {
        // 查询该用户下有哪些专家组
        Page<ExpertGroupDTO> expertGroupPage = this.baseMapper.pageExpertGroup(expertGroupDTO.pages(), expertGroupDTO);
        // 查询每个专家组中拥有的专家
        for (ExpertGroupDTO group : expertGroupPage.getRecords()) {
            List<ExpertGroupMembersDTO> membersDTOList = this.baseMapper.listExpertGroupMembers(group);
            group.setExpertList(membersDTOList);
        }
        return PageResult.get(expertGroupPage);
    }

    /**
     * 查询专家组详情
     *
     * @param expertGroupDTO
     * @return
     */
    @Override
    public ExpertGroupDTO expertGroupDetail(ExpertGroupDTO expertGroupDTO) {
        ExpertGroup group = this.getById(expertGroupDTO.getId());
        if(group==null){
            return null;
        }
        ExpertGroupDTO groupDTO = group.to(ExpertGroupDTO.class);
        String bidWayShowName = BidWayShowName(groupDTO.getBidWay());
        groupDTO.setBidWayName(bidWayShowName);


        List<ExpertGroupMembersDTO> membersDTOList = this.baseMapper.listExpertGroupMembers(group);
        groupDTO.setExpertList(membersDTOList);

        return groupDTO;
    }


    public String BidWayShowName(Integer bidWay){
        String bidWayName = "/";
        switch (bidWay){
            case 1:
                bidWayName = "公开招标";
                break;
            case 99:
                bidWayName = "邀请";
                break;
            case 2:
                bidWayName = "竞争性磋商";
                break;
            case 3:
                bidWayName = "竞争性谈判";
                break;
            case 5:
                bidWayName = "单一来源采购";
                break;
            case 7:
                bidWayName = "询价";
                break;
            default:
        }
        return bidWayName;
    }

    /**
     * 删除专家组
     *
     * @param expertGroupDTO
     * @return
     */
    @Transactional
    @Override
    public boolean removeExpertGroup(ExpertGroupDTO expertGroupDTO) {
        boolean groupResult = this.removeById(expertGroupDTO.getId());
        LambdaQueryWrapper<ExpertGroupMembers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExpertGroupMembers::getGroupId, expertGroupDTO.getId());
        boolean memberResult = expertGroupMembersService.remove(queryWrapper);
        return groupResult && memberResult;
    }

    /**
     * 查询抽取的项目是否存在
     * @author tian
     * @date 2022-4-27 10:10
     * @param expertGroupDTO
     * @return boolean
     */
    @Override
    public boolean countByProjectNameAndOrgId(ExpertGroupDTO expertGroupDTO) {
        Integer num = baseMapper.countByProjectNameAndOrgId(expertGroupDTO);
        return num > 0 ? false : true;
    }
}
